package com.asia5b.a5bmall.same.dialog

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.asia5b.a5bmall.same.R
import com.asia5b.a5bmall.same.adapter.SelectBaseAdapter
import com.asia5b.a5mall.middleware.model.bean.SelectText
import com.asia5b.a5mall.middleware.base.BaseDialog
import com.asia5b.a5mall.middleware.utils.newDivider
import kotlinx.android.synthetic.main.dialog_selectbase.*

/**
 * Created by zhangyuncai on 2020/5/27.
 */
class SelectBaseDialog(context: Context) : BaseDialog(context) {
    override val layoutId: Int=R.layout.dialog_selectbase
    override val heightPercent: Double=0.6

    var selectText: SelectText? = null
    var title: String? = null
    val mDatas = arrayListOf<SelectText>()
    val mAdapter by lazy {
        SelectBaseAdapter(mDatas)
    }
    val mRecyclerView by lazy {
        rv_selectbase
    }




    override fun initView() {
        title?.let {
            tv_selectbase_title.text = it
        }
        mAdapter.selectText = selectText
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(context)!!
            adapter = mAdapter
            addItemDecoration(newDivider(context))
        }
    }

    override fun initEvent() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val selectText = mAdapter.getItem(position) as SelectText
            if (mAdapter.selectText == null||(mAdapter.selectText!!.getSelectId() != selectText.getSelectId())) {
                mAdapter.selectText = selectText
                mAdapter.notifyDataSetChanged()
            }
        }
        bt_selectbase_sure.setOnClickListener {
            if (mAdapter.selectText != null) {
                mSureClick?.invoke(mAdapter.selectText!!)
            }
            dismiss()
        }
        tv_selectbase_close.setOnClickListener {
            dismiss()
        }
    }


}